<template>
    <!-- <div class="echart-box" id="main" style="height:1000px;width:500px;"></div> -->
    <div class="echart-box" :id="id" :style="{height:height,width:width}"></div>
</template>
<script>
import echarts from 'echarts';
import resize from './resize.js';
export default {
    mixins: [resize],
    data:()=>{
        return {
            chart:null
        }
    },
    props:{
        width:{
            type:String,
            require:true
        },
        height:{
            type:String,
            require:true
        },
        id:{
            type:String,
            require:true
        },
        dataset:{
            type:Array,
            default:()=>{
                return [];
            }
        },
        title:{
            type:String,
            require:true
        },
        seriesName:{
            type:String,
            require:true
        }
    },
    mounted(){
        this.initChart();
        console.log('dataset',this.dataset)
    },
    beforeDestroy(){
        if (!this.chart) {
            return;
        }
        this.chart.dispose();
        this.chart = null;
    },
    methods:{
        initChart(){
            let dom = document.getElementById(this.id);
                this.chart = echarts.init(dom);
                this.chart.setOption({
                    title: {
                        text: this.title,
                        left: 'center'
                    },
                    tooltip: {
                        trigger: 'item'
                    },
                    legend: {
                        orient: 'vertical',
                        left: 'right',
                    },
                    series: [
                        {
                            name: this.seriesName,
                            type: 'pie',
                            radius: '50%',
                            data: [],
                            //     {value: 1048, name: '搜索引擎'},
                            //     {value: 735, name: '直接访问'},
                            //     {value: 580, name: '邮件营销'},
                            //     {value: 484, name: '联盟广告'},
                            //     {value: 300, name: '视频广告'}
                            // ],
                            emphasis: {
                                itemStyle: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                })
                this.chart.showLoading();
        },
        resetChart(width,height){
            if(this.chart){
                this.chart.hideLoading();
                this.chart.setOption({
                    series:{
                        name:this.seriesName,
                        data:this.dataset
                    }
                })
                this.chartResize(width,height)
            }
        },
        chartResize(width,height){
            this.chart.getDom().style.width = `${width}px`;
            this.chart.getDom().style.height = `${height}px`;
            this.chart.resize();
        }
    },
    watch:{
    }
}
</script>
<style lang="scss">

</style>